<?php

include_once('connect-db.php');

/*
  Deal with HTTP request
 */
if (isset($_GET['type'])) {
  $sm_type = $_GET['type'];
}
if (isset($_GET['source_id'])) {
  $source_id = $_GET['source_id'];
}

if ($sm_type == 'edit') {
  if (!isset($source_id)) {
    die('$source_id no set');
  }

  $sql = "select * from source where id = '{$source_id}'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo mysql_error();
    return;
  }
  $result = mysql_fetch_assoc($ret);
  if (!$result) {
    return;
  }


  $item_handle = mysql_query(sprintf('select * from source_item where source_id="%s" order by id desc', $source_id));
  $source_id_rep = str_replace('.', '_', $source_id);

  $tag_buffer_id = "tag_buffer_" . $source_id_rep;
  $base_url = $result['base_url'];
  $tags = explode('#', $result['tags']);
  $source_id = $result['id'];
  $source_encoding = $result['encoding'];
  $need_kw = $result['need_query'];
  $is_prime = $result['is_prime'];
  $domain = $result['domain'];
}

?>


<div class="manage-source">

    <?php if ($sm_type == 'add') { echo "<h4>新增内容源</h4>"; } ?>
  <form action="index.php?op=<?php echo $sm_type ?>" method="post" <?php if ($sm_type == 'edit') { echo 'class="ajax-sub"'; } else { echo 'class="add-source"';} ?> >
      <ul class="basic-info">
        <li>
          <b>基本信息</b>
          <a class="toggle-basic-info" href="javascript:void(0)"
             onClick="toggle_basic_info(this)">+</a>
          <input type="submit" value="<?php if ($sm_type == 'edit') { echo '更新源'; } else { echo '添加源';} ?>" />
          <?php 
             if ($sm_type == 'edit') {
               echo sprintf('<button onclick="remove_source(this, \'%s\'); return false;">删除源</button>',
             $source_id_rep);

             }
          ?>
          <input type="checkbox" name="is_prime" <?php if(isset($is_prime) && $is_prime == 1) {echo "checked='yes'";} ?>/><label class="is_prime">PRIME</label>
          <span class="domain">
            <label>domain限制:</label>
            <input type="text" name="domain" value="<?php if (isset($domain)) { echo $domain; }?>"></input>
          </span>
        </li>
        <li>
          <label>URL：</label><input name="base_url" size="40" value="<?php if(isset($base_url)) {echo $base_url;}?>" />
          <label>编码：</label><input name="encoding" size="8" value="<?php if(isset($source_encoding)) {echo $source_encoding;}?>"/>
        </li>
        <li class="no-padding">
          <label>源ID：</label><input name="source_id" size="40" value="<?php if(isset($source_id)) {echo $source_id;}?>"/>
          <label>使用关键词：</label><input type="checkbox" name="need_kw" value="yes" <?php if (isset($need_kw) && $need_kw == 1) { echo "checked='1'"; }?> />
          <input type="input" name="keyword" size="8" value="" <?php if (!(isset($need_kw) && $need_kw == 1)) { echo 'style="display:none"'; } ?> />
        </li>
      </ul>
      
      <ul class="source_detail" style="display:none">
        <li>
          <div class="tag-div">
            标签：
            <span class="source_tag_span">
              <?php
                 if (isset($tags)) {
                 for ($i = 0; $i < count($tags); ++$i) {
                                   $tag = $tags[$i];
                                   if (strlen($tag) > 0) {
                 $current_id = "source_tag_".$source_id_rep."_".$i;

                 ?>
              <a href="javascript:void(0)" onClick="remove_source_tag(this, '<?php echo $current_id?>')">(X)</a>
              <input type="text" class="source_tag" readonly="true" name="tags[]"
                     value="<?php echo $tag?>"
                     id='<?php echo $current_id?>'/>
              <?php }}}?>
            </span>
          </div>
        </li>
      </ul>
    </form>
    
    <?php
       if ($sm_type == 'edit') { 
    ?>

      <ul class="source_detail" style="display:none">
        <li>
          <b>条目信息</b>&nbsp;&nbsp;
          <a href="javascript:void(0)" onClick="add_source_item(this)">添加条目</a>
          <span class="update-status"> </span>
        </li>
        <ul class="new_items"></ul>
           <ul class="item-ul">
           <?php 
              while ($source_item = mysql_fetch_assoc($item_handle)) {
                include("item-detail.php");
              }
           ?>
           </ul>
      </ul>
   <?php } ?>
</div>
